// 信息管理接口
import request from '@/utils/request'
import type { CategoryResData } from './categoryType'
import type { CaseResData,queryParams } from './caseType'

enum API {
  category_tree = '/api/case/category/tree',
  add_case = '/api/case/info/add',
  update_case = '/api/case/info/update',
  delete_case = '/api/case/info/delete',
  getDataByCategoryId = '/api/case/info/page',
  getQuestionDataByCaseInfoId = '/api/question/list',
  add_question = '/api/question/add',
  update_question = '/api/question/update',
  delete_question = '/api/question/delete',
}
// 获取类别树
export const reqCategoryTree = () => {
  return request.get<any, CategoryResData>(API.category_tree)
}
// 新增信息、编辑信息
export const reqAddOrUpdateCase = (data: any) => {
  let hasId = data.has('id')
  let idValue = hasId ? data.get('id') : null
  let hasValue = idValue !== null && idValue !== ''
  if (hasValue) {
    return request.put<any, any>(API.update_case, data)
  } else {
    return request.post<any, any>(API.add_case, data)
  }
}
// 删除单个信息
export const reqRemoveCase = (id: number, version: number) => {
  return request.delete<any, any>(API.delete_case + `?id=${id}&version=${version}`)
}
// 根据类别categoryId查询信息表格数据
export const reqGetDataByCategoryCode = (params: queryParams) => {
  const queryParams = new URLSearchParams({
    current: params.current.toString(),
    size: params.size.toString(),
    categoryId: params.categoryId,
    name: params.name || ''
  }).toString()
  const url = `${API.getDataByCategoryId}?${queryParams}`
  return request.get<any, CaseResData>(url)
}

// 根据病例caseInfoId查询问诊数据
export const reqGetQuestionDataByCaseInfoId = (caseInfoId:string) => {
  return request.get<any,any>(API.getQuestionDataByCaseInfoId + `?caseInfoId=${caseInfoId}`)
}
// 新增/编辑问诊
export const reqAddOrUpdateQuestion = (data: any) => {
  if (data.id) {
    return request.put<any, any>(API.update_question, data)
  } else {
    return request.post<any, any>(API.add_question, data)
  }
}
// 删除单个问诊
export const reqRemoveQuestion = (id: number, version: number) => {
  return request.delete<any, any>(API.delete_question + `?id=${id}&version=${version}`)
}
